<?php
    //require the cookie information
    //view based
    require_once(SYS_CONFIG . '/lang.php');

    function setLang($langName = DEFAULT_LANG){
        global $__langSetting;
        if ( !in_array($langName, $__langSetting) ){
            $langName = DEFAULT_LANG;
        }
        if ( !isset( $_SESSION ) ) session_start();
        $_SESSION['lang'] = $langName;
        return $langName;
    }

    function loadLang(){
        if ( !isset( $_SESSION ) ) session_start();
        if ( !isset( $_SESSION['lang'] ) ) {
            return setLang();
        }
        else {
            return $_SESSION['lang'];
        }
    }

    function transLang($viewName){
        $lang = loadLang();
        return "$viewName.$lang";
    }
    
    function langInit(){
        if (!isset($_SESSION)) session_start();
        if (!isset($_SESSION['lang']) && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
            if (false !== stripos($_SERVER['HTTP_ACCEPT_LANGUAGE'],'cn'))
                setLang('ch');
            elseif (false !== stripos($_SERVER['HTTP_ACCEPT_LANGUAGE'], 'en'))
                setLang('en');
            else
                setLang();
        }
    }

    langInit();

